//
//  QrCodeView.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/22.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "QrCodeView.h"

@implementation QrCodeView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _backView = [[UIImageView alloc]initWithFrame:frame];
        _backView.image = [UIImage imageNamed:@"shareBackImg"];
        [self addSubview:_backView];
        [self addChildViewWithFatherView:_backView];
    }
    return self;
}
- (void)addChildViewWithFatherView:(UIView *)fatherView {
    UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - 87) / 2, 47, 87, 104)];
    icon.image = [UIImage imageNamed:@"shareIcon"];
    [fatherView addSubview:icon];
    
    UIImageView *beside = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - 288) / 2, 184, 288, 75)];
    beside.image = [UIImage imageNamed:@"beside"];
    [fatherView addSubview:beside];
}
//生成带 logo的二维码
-(void)logoQrCode:(UIImageView *)imgV {
    NSArray *filter = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
    NSLog(@"%@", filter);
    // 二维码过滤器
    CIFilter *filterImage = [CIFilter filterWithName:@"CIQRCodeGenerator"];https:
    // 将二位码过滤器设置为默认属性
    [filterImage setDefaults];
    NSUserDefaults *USER = [NSUserDefaults standardUserDefaults];
    //二维码地址,此二维码不能支付,需要配合服务器来二维码的地址(跟后台人员配合)
    NSString *urlStr  = [USER objectForKey:@"codeUrl"];
    // 将文字转化为二进制
    NSData *dataImage = [urlStr dataUsingEncoding:NSUTF8StringEncoding];
    // 打印输入的属性
    NSLog(@"%@", filterImage.inputKeys);
    // KVC 赋值
    [filterImage setValue:dataImage forKey:@"inputMessage"];
    // 取出输出图片
    CIImage *outputImage = [filterImage outputImage];
    outputImage = [outputImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
    // 转化图片
    UIImage *image = [UIImage imageWithCIImage:outputImage];
    
    //为二维码加自定义图片 开启绘图, 获取图片 上下文<图片大小>
    UIGraphicsBeginImageContext(image.size);
    // 将二维码图片画上去
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    // 将小图片画上去
    UIImage *sImage = [UIImage imageNamed:@"share"];
    CGFloat sImageW = image.size.width / 5;
    CGFloat sImageH = sImageW;
    CGFloat sImageX = (image.size.width - sImageW) * 0.5;
    CGFloat sImgaeY = (image.size.height - sImageH) * 0.5;
    [sImage drawInRect:CGRectMake(sImageX, sImgaeY, sImageW, sImageH)];
    // 获取最终的图片
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭上下文
    UIGraphicsEndImageContext();
    //设置图片
    _erWeiMa = [[UIImageView alloc] init];
    _erWeiMa.image = finalImage;
    [imgV addSubview:_erWeiMa];
    [_erWeiMa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY).offset(130);
        make.width.mas_equalTo(159);
        make.height.mas_equalTo(159);
    }];
//    [self layoutIfNeeded];
}
@end
